<?php

namespace app\index\controller;

use app\index\model\Goods;
use think\Controller;
use think\Db;

class DashboardCtl extends Controller
{
    /*
     * 单品销售榜
     */
    public function singleProductSalesChart()
    {
        $res['data'] = Goods::order('payments', 'desc')->limit(10)->select();
        $res['code'] = 20000;
        return json($res);
    }

    /*
     * 分类销售榜
     */
    public function categorySalesChart()
    {
        $res['data'] = Db::query("SELECT sc.name, SUM(payments) AS pay_sum FROM `goods` INNER JOIN sec_classification sc ON goods.classification_id = sc.id GROUP BY classification_id ORDER BY pay_sum DESC LIMIT 8");
        $res['code'] = 20000;
        return json($res);
    }

    /*
     * 近七天销售额
     */
    public function pastSevenDaysSalesChart()
    {
        $res['data'] = Db::query("SELECT FROM_UNIXTIME(pay_time,'%Y-%m-%d') AS days, SUM(tprice) AS tprice FROM (SELECT * FROM `orders` WHERE DATEDIFF(now() , FROM_UNIXTIME(`pay_time`)) < 7) AS temp GROUP BY days");
        $res['code'] = 20000;
        return json($res);
    }
}